package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class ZhuCeActivity extends AppCompatActivity {

    private EditText et_phone;
    private EditText et_password;
    private Button bt_tijiao;
    private EditText et_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhu_ce);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        et_phone = findViewById(R.id.et_phone);
        et_password = findViewById(R.id.et_passwold);
        bt_tijiao = findViewById(R.id.bt_tj);
        et_name = findViewById(R.id.et_mima2);
    }
    private void initData() {
        String url = "http://10.1.62.39/api/user/login";
        String telephone = et_phone.getText().toString().trim();
        String password = et_password.getText().toString().trim();
        String name = et_name.getText().toString().trim();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("telephone",telephone);
            jsonObject.put("name",name);
            jsonObject.put("password",password);

        }catch (JSONException e){
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Toast.makeText(ZhuCeActivity.this,jsonObject.optString("msg")+"",Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(ZhuCeActivity.this,"网络请求失败!",Toast.LENGTH_SHORT).show();
            }
        });
        RequestQueue queue= Volley.newRequestQueue(ZhuCeActivity.this);
        queue.add(jsonObjectRequest);
    }
    private void initEvent() {
        bt_tijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
                Intent intent=new Intent(ZhuCeActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }
}